Subject:	Article for MegaMag8: Converting games to cartridge

Converting games to Cartridge
Written by: Nir Dary 23/6/2001
ndary@bigfoot.com

We all love cartridges some of us even collect them. They are simple and quick to load, just plug it in you computer and the game starts.

This time I would like to share my knowledge and show you all how you can convert your favorite games to cartridges, all you need is an Empty EPROM chip, an EPROM burner, Cartridge PCB and an Assembler editor.

Before we will start to explain how to port a disk/tape games lets have few words about cartridges...

The earlier ATARI model (800) had two 8K cartridges slot: Left and Right. The Right cartridge occupied memory address $8000 to $9FFF while the Left cartridge occupied memory address $A000 to $BFFF. Once a cartridge was inserted to the computer the memory address above becomes read only data. The XL/XE ATARI models are compatible with the 800 cartridges but it is also have the possibility to read one 16K cartridge (that will occupy memory address $8000 to $BFFF), lets not forget the Bank Switchable cartridges, Cartridges that contain more then 16K data like the XEGS or OSS cartridges. The XEGS can contain up to 128K of game data i.e. ‘Ace of Acers’, ‘light Simulator 2’ carts etc..

Lets look at the cartridge connector:

Cartridge Slot ("Left" slot on all machines; "Right" slot on 800 only):

A  B  C  D  E  F  H  J  K  L  M  N  P  R  S
1	2  3  4  5  6  7  8  9 10 11 12 13 14 15

 1. /S4                        A. RD4
 2. A3                         B. GND
 3. A2                         C. A4
 4. A1                         D. A5
 5. A0                         E. A6
 6. D4                         F. A7
 7. D5                         H. A8
 8. D2                         J. A9
 9. D1                         K. A12
10. D0                         L. D3
11. D6                         M. D7
12. /S5                        N. A11
13. +5V                        P. A10
14. RD5                        R. R/W
15. /CCTL                      S. B02

When the ATARI computer is been booted one if the first things the Operating system is checking is cartridge presence. If a cartridge is inserted its been executed, so a cartridge can be executed even before the rest of the OS routines are finished (i.e a Diagnostic Cartridge)

The Last 6 bytes of the cartridge Memory location (addresses $BFFA to $BFFF) determine the RUN/INI address of the cartridge, Load DOS before running the cart software? etc…

$BFFA/$BFFB - is the cartridge start address.
$BFFC - A non-zero number here tells the OS that there is no cartridge
        in the left slot
$BFFD – set to $05 if you want to boot Dos before running the cart $04 
        if no
$BFFE/$BFFF - Cartridge initialization address

The XEGS cartridges contains several 8K banks (depend on the cartridges size), the last bank is always shown at Memory area $A000 to $BFFF of the Atari computer while the rest of the 8K banks are mapped memory area $8000 to $9FFF depending on what value of data you set $D5XX. For example on a 64k cartridge the last 8k Bank of the cartridge (cartridge ROM memory location $E000 to $FFFF) are mapped to the XL memory location $A000 to $BFFF, and when accessing Atari location $D500 you can set which of the first seven 8K banks will be mapped to the ATARI memory location $8000 TO $9FFF. 

Now lets get into business…

You cannot convert any game to a cartridge there are limitations... 

1.	You must feet the game into the cartridge memory space you can use
A 16k PCB for games under are less then 16k in size, or XEGS pcb with larger ROMs.
2.	The game must load in one part! If the game is been loaded in
   several stages without modifying the game code it will try 
   accessing TAPE/DISK when looking for the next stage to load 
3.	The game should not try to write to the cartridge memory address.
Because after you insert the cartridge this are is read only memory
4.	Some games are detecting cartridge presence and when a cartridge is
   been inserted the game will not run, this was done to prevent from 
   some debugging cartridges to help hacking the game code

If your game is not restricted by the above limitation then we have a go. What we will go is simply write a routine that will copy the game data from the ATARI Cartridge Memory area back to the original game location and execute the game. This works for 90% of the games I tried

Next you will find a generic cartridge memory dumping routine that will work for most of the games. To explain how it works let take a game that and make it work from a 16k cartridge PCB, (“Danger Ranger” was never released on a cartridge)

Danger Ranger game loads into Memory Address: $0400 to $211F and been executed at address: $0488

The TABLE at the beginning of the source tell the copy routine to copy the Game Data from Address $A200-$BF20 To $0400-$211F. here is the TABLE description:

Byte 0   – What 8k bank to access, to use with XEGS carts (on 16k cart 
           it should always set to 00), if equal to $FF then end copy.
Byte 1,2 – Start Source address of data location on the Cartridge ROM, 
           (on 16k cart pcb it will be from $8000 to $BFFF, and on an
            XEGS cart from $A000 to $BFFF)
Byte 3,4 – Destination address where to copy the game data.
Byte 5,6 – Last byte of Source address to copy

;Universal Cartrige Copy / Run routine
;written by Nir Dary 13/6/2001
;
;
GSA       = $0488 ;Game Start Adress
DOSINI    = $0C ;
DOSVEC    = $0A ;
TMPFROM   = $CA ;Tmp Source address
TMPTO     = $CC ;Tmp Dest. address
TMPEND    = $CE ;Tmp End Copy Addr
TMPTABLE  = $D0 ;Table address
;
; This Routine will be located at $A000
; but Assembled at $3000 mem area
;
        Org $3000 
;
TABLE .BYTE $00,$00,$A2,$00,$04,$20,$BF
      .BYTE $FF  ;$FF ends the copy 
;
START   STX $03E9 ;Cassette vector -      
        STX $03EA ;disabled
        DEX
        STX $D301 ;Basic off - hardware
        LDA #$01
        STA $09   ;Boot? - say booted
        STA $42   ;Critic set 
        STA $03F8 ;Basic off - software
;
;
        LDA #$00  ;Short Move Routine
        STA TMPTO
        STA TMPFROM
        STA TMPTABLE
        TAY
        LDA #$30  ;Copy to PAGE 30
        STA TMPTO+1
        STA TMPTABLE+1
        LDA #$A0
        STA TMPFROM+1
L1      LDA (TMPFROM),Y
        STA (TMPTO),Y
        INY
        BNE L1
        JMP COPY ;ie. jmp 3000 region to
                 ;move code out of cart.
;
;
COPY    LDA #$00  ;Enable Access to
        STA $D40E ;Shadow O.S Ram
        SEI
        LDA #$FE
        STA $D301
L2      LDY #$00  ;Load Bank# from table 
                  ;into X register
        LDA (TMPTABLE),Y ;deted if End 
        CMP #$FF      ;of Table then 
        BEQ EXITCOPY  ;exit to run game
        TAX
        INC TMPTABLE

        LDA (TMPTABLE),Y  ;Load Source 
        STA TMPFROM       ;Address
        INC TMPTABLE
        LDA (TMPTABLE),Y
        STA TMPFROM+1
        INC TMPTABLE

        LDA (TMPTABLE),Y  ;Load Dest 
        STA TMPTO         ;Address
        INC TMPTABLE
        LDA (TMPTABLE),Y
        STA TMPTO+1
        INC TMPTABLE

        LDA (TMPTABLE),Y   ;Load Source 
        STA TMPEND         ;end Address 
        INC TMPTABLE       ;to copy
        LDA (TMPTABLE),Y
        STA TMPEND+1
        INC TMPTABLE
;
;Start of Copy Routine
;
MAINLOOP  STX $D500     ;Load Rom bank to
        LDA (TMPFROM),Y
        STA (TMPTO),Y
        INC TMPFROM
        BNE L3
        INC TMPFROM+1
L3      INC TMPTO
        BNE L4
        INC TMPTO+1
L4      LDA TMPFROM+1    
        CMP TMPEND+1
        BNE MAINLOOP
        LDA TMPFROM
        CMP TMPEND
        BNE MAINLOOP
        BEQ L2          ; Do next Bank?
;
EXITCOPY  LDA #$22
        STA $D400  ;DMA control
        LDA #$FE
        STA $D301  ;RAM under ROM on
        LDA #$40   ;Some games need $C0
        STA $D40E  ;NMI allowed
        CLI

        LDA #<GSA
        STA DOSINI
        STA DOSVEC
        LDA #>GSA
        STA DOSINI+1
        STA DOSVEC+1
;
        LDA #$C0    ;C000 high byte
        STA $2E4    ;system size
        STA $2E6    ;
        STA $6A     ;dlist ends a
;
        LDA #$00    ;cart interlock
;       STA $03FA   ;no cart status = off
        STA $42     ;critic OK
        STA $244    ;COLDST
        LDA #$01
        STA $08     ;WARMST
        TAY
        CLC
        JMP GSA     ;$E474
        RTS

;
;Cartrige Run Address
;See Mapping the Atari
;
        Org $BFFA
          .WORD $0000
          .BYTE $00
          .BYTE $04
          .WORD START+$7000

Once you assembled this routine make sure you add the game data to the correct source location. With is doc you will find a ready made .ROM file of Danger Ranger game, simply burn the ROM data into a 27C64 (8k) EPROM, and insert it on the Right slot of a 16k cartridge pcb, (you the 16k Cartridges PCB from B&C computer vision or Best Electronics.